<template>
    <div class="index-lists">
        <el-card class="!border-none mt-4" shadow="never">
            <div class="upload-btn">
                <el-button v-perms="['information:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column label="版本编号" prop="versionId" min-width="100" />
              <el-table-column label="版本类型" prop="versionType" min-width="100">
                <template #default="{ row }">
                  <span v-if="row.versionType === 0">安卓</span>
                  <span v-else-if="row.versionType === 1">ios</span>
                </template>
              </el-table-column>
                <el-table-column label="版本名称" prop="versionName" min-width="100" />
                <el-table-column label="版本更新内容" prop="versionUpdateContent" min-width="100" />
                <el-table-column label="下载地址" prop="downloadAddress" min-width="100" />
                <el-table-column label="暂不发布/发布" prop="versionStatus" min-width="100">
                    <template #default="{ row }">
                        <dict-value :options="dictData.数字类型" :value="row.versionStatus" />
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['information:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['information:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.versionId)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            :dict-data="dictData"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="information">
import { informationDelete, informationLists } from '@/api/information'
import { useDictData } from '@/hooks/useDictOptions'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import type { UploadInstance } from 'element-plus'
import {getToken} from "@/utils/auth";

const uploadRef = ref<UploadInstance>()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
})
const token = getToken()
const headers = ref({token});

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: informationLists,
    params: queryParams
})

const { dictData } = useDictData<{
    数字类型: any[]
}>(['数字类型'])


const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (versionId: number) => {
    await feedback.confirm('确定要删除？')
    await informationDelete({ versionId })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
<style>
.upload-btn{
  display: flex;
  align-items: center;
}
.upload-demo{
  margin-top: 10px;
  margin-left: 15px;
}
</style>
